using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using UnityEngine;
using UnityEngine.UI;

public class LittleHead : MonoBehaviour
{
    public Text nameText;
    public Text levelText;
    public Image headImg;
    public GameObject selectedImg;
    public GameObject maskImg;
    public GameObject Img2000;
    private int id;

    void Start()
    {
        GlobalEventManager.OnEvent<int>("UpdateLevelInfo", OnUpdateLevel);
    }

    void OnDestroy()
    {
        GlobalEventManager.OffEvent<int>("UpdateLevelInfo", OnUpdateLevel);
    }

    public void OnClick()
    {
        if (maskImg.activeSelf)
        {
            Tools.ShowTipToast("You don't have enough coins to upgrade.");
            return;
        }
        GlobalEventManager.EmitEvent<int>("UpdateHeadInfo", id);
        selectedImg.SetActive(true);
        
        Character.Instance.currentIndex = id;
    }

    public void InitHead(int index)
    {
        id = index;
        JObject data = Data.touxiangpeizhi[index].ToObject<JObject>();
        if (data == null) return;
        nameText.text = data["name"].ToString();
        headImg.sprite = ResManager.Instance.GetHeadIcons()[index];

        selectedImg.SetActive(false);
        if (index > 3)
        {
            Img2000.SetActive(true);
            maskImg.SetActive(true);
            levelText.text = "lv.1";
        }
        else
        {
            Img2000.SetActive(false);
            maskImg.SetActive(false);
            levelText.text = "lv." + Character.Instance.GetPersonLevel(index);
        }

        if (index == Character.Instance.currentIndex)
        {
            GlobalEventManager.EmitEvent<int>("UpdateHeadInfo", index);
            selectedImg.SetActive(true);
        }
    }

    public void OnUpdateLevel(int index)
    {
        if (index == id)
        {
            levelText.text = "lv." + Character.Instance.GetPersonLevel(index);
            selectedImg.SetActive(true);
        }
    }
}
